package jxau.jwgl.web.controller;

import jxau.jwgl.commons.WebData;
import jxau.jwgl.commons.entity.CourseCategory;
import jxau.jwgl.commons.entity.Semester;
import jxau.jwgl.commons.entity.User;
import jxau.jwgl.service.CommonDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * CommonDataController
 *
 * @author LongShu 2017/06/09
 */
@RestController
@RequestMapping("/commonData")
public class CommonDataController extends BaseController {

    @Autowired
    private CommonDataService commonDataService;

    @GetMapping("/semesterList")
    public Object semesterList(HttpServletRequest request, Integer limit) {
        User loginUser = getLoginUser(request);
        logger.debug("limit:{}", limit);

        List<Semester> semesterList = commonDataService.semesterList(loginUser.getUsername());
        if (limit != null && limit > 0 && semesterList.size() > limit) {
            semesterList = semesterList.subList(0, limit);
        }
        return WebData.newObj(semesterList);
    }

    @GetMapping("/courseCategory")
    public Object courseCategory(HttpServletRequest request) {
        User loginUser = getLoginUser(request);

        List<CourseCategory> courseCategoryList = commonDataService.courseCategory(loginUser.getUsername());
        return WebData.newObj(courseCategoryList);
    }

}
